package Mash.UI;

/*Copyright (c)2009  Sergey Schekochikhin (Genocidicbunny)
 *created on Jul 25, 2009 1:18:56 AM
 */
import Mash.MainGame;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class MainMenu extends JPanel implements MouseListener, KeyListener, Runnable {
	private int xsize, ysize;
    private SelectedMenu CURRENTMENU = SelectedMenu.CAMPAIGN;
	private SelectedMenu ABOVECURRENT = SelectedMenu.QUIT, BELOWCURRENT = SelectedMenu.WARROOM;
	private Thread pulsate;
	int i = 0;
	//private int currentArrowAlpha = 200;
	private boolean running = false;
	private boolean upBounce = true;
	//private boolean rotating = false;
	//private boolean backgrounddrawn;
	private BufferedImage background, logo;
	private BufferedImage campaign, warroom, options, quit, currentButton, aboveButton, belowButton;
	private MainGame myMain;
	
   	public void mouseClicked( MouseEvent e ) {
           requestFocusInWindow();
    }
	public void mousePressed( MouseEvent e ) {}
	public void mouseReleased( MouseEvent e ) {}
	public void mouseEntered( MouseEvent e ) {}
	public void mouseExited( MouseEvent e ) {}
	public void keyTyped( KeyEvent e ) {}
    public void keyPressed( KeyEvent e ) {
        int kcode = e.getKeyCode();
        //rotates the menu up by calling the rotateMenu method
        //System.out.println("Key Pressed");
        if(kcode == KeyEvent.VK_UP){
            rotateMenu(true);
        }
        //likewise, rotates down
        else if(kcode == KeyEvent.VK_DOWN){
            rotateMenu(false);
        }
		//temporarily here so we can exit the program easier
		else if(kcode == KeyEvent.VK_ESCAPE)
			System.exit(6);
		else if(kcode == KeyEvent.VK_ENTER){
			if(CURRENTMENU == SelectedMenu.CAMPAIGN){
				//will use this to start a new game later
				myMain.startGame();
				SwingUtilities.getWindowAncestor(this).dispose();
				System.out.println("Starting new game");
				//System.exit(7);
			}
			else if(CURRENTMENU == SelectedMenu.WARROOM){
				//will use this to start vs matches later
				System.exit(8);
			}
			else if(CURRENTMENU == SelectedMenu.OPTIONS){
				//will use this to access options if they are available
				myMain.showOptions();
			}
			else if(CURRENTMENU == SelectedMenu.QUIT){
				System.exit(10);
			}
		}

        repaint();
    }
    public void keyReleased( KeyEvent e ) {}
    public MainMenu(Dimension d){
		super();
		ysize = (int)d.getHeight();xsize =(int)d.getWidth();
		addKeyListener(this);
		addMouseListener(this);
		System.out.println(d);
		setVisible(true);
		setPreferredSize(d);
        requestFocusInWindow();
        System.out.println(hasFocus());
		super.addNotify();
		startMenu();
		System.out.println(System.getProperty("user.dir"));
		//image load block
		try{
			//overlay = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\glassoverlay.png"));
			background = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\background.jpg"));
			logo = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\logo.png"));
			campaign = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\campaignbutton.png"));
			warroom = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\warroombutton.png"));
			options = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\optionsbutton.png"));
			quit = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\quitbutton.png"));
		}catch( IOException e){e.printStackTrace();System.exit(99);}
		//backgrounddrawn = false;
		currentButton = campaign;
		aboveButton = quit;
		belowButton = warroom;

		
	}
	public void paintComponent( Graphics g){
		super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
		//g2d.setColor(new Color(204, 153, 51));
		//g2d.fillRect(0,0, xsize, ysize);
		//if(!backgrounddrawn){
			g2d.drawImage(background, 0, 0, xsize, ysize, null);
			//backgrounddrawn = true;
		//}
		g2d.drawImage(logo, 0, 0, 300,100, null);
		drawSelected(g2d);
		drawArrows(g2d);
	}
	//simply paints a bordered rectangle of the currently selected menu uption's color and then writes its name on top.
	//TODO:will in the future use images for each of the buttons.
    private void drawSelected(Graphics2D g2d){
		//Makes the menu button bounce up and down slightly to give it an emphasis.
		if(i >=9)
			upBounce = false;
		if(i == 0)
			upBounce = true;
		if(upBounce)
			i = (i+1)%10;
		else
			i = (i-1)%10;
		//draws the button images and their drop shadows which are created using round rect's with a low opacity
		g2d.setColor(new Color(0,0,0,100));
		g2d.fillRoundRect(xsize/2-66,ysize/2-194, 144, 64,2,2);
		g2d.drawImage(aboveButton, xsize/2-72,ysize/2-200, 144, 64,null);
		g2d.fillRoundRect(xsize/2-80,ysize/2-30-i, 180, 80,3,3);
		g2d.drawImage(currentButton, xsize/2-90,ysize/2-40-i, 180, 80,null);
		g2d.fillRoundRect(xsize/2-66,ysize/2+126, 144, 64,2,2);
		g2d.drawImage(belowButton, xsize/2-72,ysize/2+120, 144, 64,null);
		//draws the help string for CURRENTMENU
		g2d.setFont(new Font("Arial", Font.ITALIC, 30));
		FontMetrics fm = g2d.getFontMetrics();		
		g2d.setColor(new Color(250,250,250, 120));
		g2d.fillRoundRect(0, ysize-80, xsize, 80, 2,2);
		g2d.setColor(Color.BLACK);
        //System.out.println(fm.stringWidth(CURRENTMENU.getText())+ " " + xsize/2);
		g2d.drawString(CURRENTMENU.getText(),xsize/2-(fm.stringWidth(CURRENTMENU.getText()))/2, ysize-10-(fm.getAscent() + (0 - (fm.getAscent() + fm.getDescent())) / 2));

    }
	//draws arrows above and below the currently selected menu button
	private void drawArrows(Graphics2D g2d){
		g2d.setColor(new Color(255,218,158, 200));
		int[] x1 = {xsize/2-25, xsize/2, xsize/2+25};
		int[] y1 = {ysize/2-60, ysize/2-100, ysize/2-60};
		int[] y2 = {ysize/2+60, ysize/2+100, ysize/2+60};
		g2d.fill(new Polygon(x1,y1,3));
		g2d.fill(new Polygon(x1,y2, 3));
	}
	//calls the enum switch methods depending on the direction we need to rotate.
	//Additionally sets the images for all buttons as appropriate.
    private void rotateMenu(boolean up){

		// rotating = true;
		if(up)
            CURRENTMENU = SelectedMenu.getPrevious(CURRENTMENU);
        else
            CURRENTMENU = SelectedMenu.getNext(CURRENTMENU);
		//selects the current button image based to CURRENTMENU
		ABOVECURRENT = SelectedMenu.getPrevious(CURRENTMENU);
		BELOWCURRENT = SelectedMenu.getNext(CURRENTMENU);
		currentButton = selectButtonImage(CURRENTMENU);
		aboveButton = selectButtonImage(ABOVECURRENT);
		belowButton = selectButtonImage(BELOWCURRENT);
    }
	//selects the current menu image based on the passed enum
	private BufferedImage selectButtonImage(SelectedMenu s){
		switch(s){
			case CAMPAIGN: return campaign;
			case WARROOM: return warroom;
			case OPTIONS: return options;
			case QUIT: return quit;
		}
		return null;
	}
	public void setMain(MainGame m){
		myMain = m;
	}
    public void run(){
		//running = true;
		while(running){
			repaint();
			try{Thread.sleep(100);}catch(InterruptedException ex){System.out.println("Oh no. I told them this would happen. Gordon, get out of there!");}
		}
	}
	public void startMenu(){
		if(pulsate == null){
			pulsate = new Thread(this);
			pulsate.start();
			running = true;
		}
	}
}